package com.itheima.controller;

import com.itheima.domain.Record;
import com.itheima.domain.User;
import com.itheima.entity.PageResult;
import com.itheima.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jca.cci.core.RecordCreator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/record")
public class RecordController {

    @Autowired
    private RecordService recordService;

    @RequestMapping("/searchRecords")
    public ModelAndView searchRecords(Record record,
                                      HttpServletRequest request,
                                      Integer pageNum,
                                      Integer pageSize){

        //当前用户信息

        User user = (User) request.getSession().getAttribute("USER_SESSION");
        //分页初始默认值
        if (pageNum==null){
            pageNum=1;
        }
        if (pageSize==null){
            pageSize=10;
        }

    ModelAndView modelAndView = new ModelAndView();
        PageResult pageResult = recordService.searchRecord(record, user, pageNum, pageSize);

        //页面
        modelAndView.setViewName("record");


        //数据
        //回显搜索数据
        modelAndView.addObject("search",record);
        //分页信息
        modelAndView.addObject("pageResult",pageResult);
        //当前页面数
        modelAndView.addObject("pageNum",pageNum);
        //地址
        modelAndView.addObject("gourl",request.getRequestURI());


        return modelAndView;
    }
}
